package com.zmn.oms.services.impl.masterworking;

import com.zmn.oms.model.entity.masterworking.MasterWorking;
import com.zmn.oms.model.entity.masterworking.MasterWorkingQuery;
import com.zmn.oms.persistence.interfaces.masterworking.MasterWorkingDao;
import com.zmn.oms.services.interfaces.masterworking.MasterWorkingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：工程师进行中工单
 *
 * @author lujia
 * @date 2018/11/05 9:07
 */
@Slf4j
@Service
public class MasterWorkingServiceImpl implements MasterWorkingService {

    @Resource
    private MasterWorkingDao masterWorkingDao;

    @Override
    public List<MasterWorking> listMasterWorkingByQuery(MasterWorkingQuery masterWorkingQuery) {
        Objects.requireNonNull(masterWorkingQuery.getMasterId());
        return masterWorkingDao.listMasterWorkingByQuery(masterWorkingQuery);
    }

    @Override
    public List<MasterWorking> listMasterWorkingByMasterId(Integer masterId) {
        return masterWorkingDao.listMasterWorking(masterId);
    }

    @Override
    public List<MasterWorking> listMasterWorkingByWorkIdList(List<Long> workIdList) {
        return masterWorkingDao.listMasterWorkingByWorkIdList(workIdList);
    }

    @Override
    public MasterWorking findMasterWorkingByWorkId(Long workId) {
        return masterWorkingDao.findMasterWorkingByWorkId(workId);
    }

    @Override
    public void updateMasterWorkingStatus(Long workId, Integer status) {
        MasterWorking masterWorking = new MasterWorking();
        masterWorking.setWorkId(workId);
        masterWorking.setStatus(status);
        masterWorkingDao.updateMasterWorkingByKey(masterWorking);
    }

    @Override
    public void updateMasterWorkingStatus(Long workId, Integer delivery, Integer waitPart, Integer waitCancel) {
        MasterWorking masterWorking = new MasterWorking();
        masterWorking.setWorkId(workId);
        masterWorking.setDelivery(delivery);
        masterWorking.setWaitPart(waitPart);
        masterWorking.setWaitCancel(waitCancel);
        masterWorkingDao.updateMasterWorkingByKey(masterWorking);
    }

    @Override
    public int updateMasterWorking(MasterWorking masterWorking) {
        log.info("updateMasterWorking masterWorking={}", masterWorking);
        return masterWorkingDao.updateMasterWorkingByKey(masterWorking);
    }

    @Override
    public void updateWorkingDutyTimeByKey(MasterWorking masterWorking) {
        masterWorkingDao.updateWorkingDutyTimeByKey(masterWorking);
    }

    @Override
    public void updateMasterWorkingChannel(Long workId, Integer channelId, String channelName, Integer servItemType) {
        MasterWorking masterWorking = new MasterWorking();
        masterWorking.setWorkId(workId);
        masterWorking.setChannelId(channelId);
        masterWorking.setChannelName(channelName);
        masterWorking.setServItemType(servItemType);
        masterWorkingDao.updateMasterWorkingByKey(masterWorking);
    }

    @Override
    public void saveMasterWorking(MasterWorking masterWorking) {
        log.info("updateMasterWorking saveMasterWorking={}", masterWorking);
        Objects.requireNonNull(masterWorking.getWorkId());
        MasterWorking dbMasterWorking = masterWorkingDao.findMasterWorkingByWorkId(masterWorking.getWorkId());
        if (dbMasterWorking == null) {
            masterWorkingDao.insertMasterWorking(masterWorking);
        } else {
            masterWorkingDao.updateMasterWorkingByKey(masterWorking);
        }
    }

    @Override
    public boolean deleteMasterWorking(Long workId) {
//        MasterWorking dbMasterWorking = masterWorkingDao.findMasterWorkingByWorkId(workId);
//        if (dbMasterWorking == null) {
//            return false;
//        }

        int ret = masterWorkingDao.deleteMasterWorking(workId);
        return ret > 0;
    }

    @Override
    public int updateWorkingWaitSchedulingByKey(MasterWorking masterWorking) {
        return masterWorkingDao.updateWorkingWaitSchedulingByKey(masterWorking);
    }

    @Override
    public int updateRedistributeStatusByKey(MasterWorking masterWorking) {
        return masterWorkingDao.updateRedistributeStatusByKey(masterWorking);
    }
}
